<template>
  <div class="theme-toggle">
    <div class="theme-slider" @click="handleSliderClick">
      <div class="slider-track">
        <div class="slider-thumb" :class="thumbPosition"></div>
        <div class="slider-icons">
          <div class="icon-light">☀️</div>
          <div class="icon-auto">⚙️</div>
          <div class="icon-dark">🌙</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useThemeStore } from '../shared/theme.ts'

const themeStore = useThemeStore()

// 滑块位置
const thumbPosition = computed(() => {
  switch (themeStore.theme) {
    case 'light':
      return 'position-light'
    case 'dark':
      return 'position-dark'
    case 'auto':
      return 'position-auto'
    default:
      return 'position-auto'
  }
})

// 处理滑块点击
const handleSliderClick = (event: MouseEvent) => {
  const slider = event.currentTarget as HTMLElement
  const rect = slider.getBoundingClientRect()
  const clickX = event.clientX - rect.left
  const sliderWidth = rect.width
  
  // 根据点击位置决定状态
  const position = clickX / sliderWidth
  
  if (position < 0.33) {
    // 左侧：浅色模式
    themeStore.setTheme('light')
  } else if (position > 0.67) {
    // 右侧：深色模式
    themeStore.setTheme('dark')
  } else {
    // 中间：跟随系统
    themeStore.setTheme('auto')
  }
}

onMounted(() => {
  themeStore.initTheme()
})
</script>

<style scoped>
.theme-toggle {
  display: flex;
  align-items: center;
  height: 32px;
}

.theme-slider {
  width: 80px;
  height: 32px;
  cursor: pointer;
  user-select: none;
}

.slider-track {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--el-fill-color-light);
  border: 1px solid var(--el-border-color-light);
  border-radius: 16px;
  transition: all 0.3s ease;
  overflow: hidden;
}

.slider-track:hover {
  border-color: var(--el-color-primary-light-5);
  box-shadow: 0 0 0 2px var(--el-color-primary-light-9);
}

.slider-thumb {
  position: absolute;
  top: 2px;
  width: 24px;
  height: 24px;
  background: var(--el-color-primary);
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  z-index: 2;
}

.slider-thumb.position-light {
  left: 2px;
}

.slider-thumb.position-auto {
  left: 28px; /* (80-24-4)/2 + 2 = 28 */
}

.slider-thumb.position-dark {
  left: 54px; /* 80-24-2 = 54 */
}

.slider-icons {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  z-index: 1;
}

.slider-icons > div {
  font-size: 12px;
  opacity: 0.6;
  transition: opacity 0.2s ease;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 深色模式适配 */
.dark .slider-track {
  background: var(--el-fill-color-dark);
  border-color: var(--el-border-color);
}

.dark .slider-track:hover {
  border-color: var(--el-color-primary-light-5);
}

.dark .slider-thumb {
  background: var(--el-color-primary);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .theme-slider {
    width: 70px;
    height: 28px;
  }
  
  .slider-thumb {
    width: 20px;
    height: 20px;
    top: 2px;
  }
  
  .slider-thumb.position-light {
    left: 2px;
  }
  
  .slider-thumb.position-auto {
    left: 25px; /* (70-20-4)/2 + 2 = 25 */
  }
  
  .slider-thumb.position-dark {
    left: 48px; /* 70-20-2 = 48 */
  }
  
  .slider-icons {
    padding: 0 6px;
  }
  
  .slider-icons > div {
    font-size: 10px;
  }
}
</style>